//
//  Contacts.m
//  A One POS
//
//  Created by AonePOS on 24/09/13.
//  Copyright (c) 2013 AonePOS. All rights reserved.
//

#import "Contacts.h"
#import "Settings.h"
#import "Home.h"
#import "Profile.h"
#import "Inventory.h"
#import "Customers.h"
#import "Employees.h"
#import "Reports.h"
#import "Stores.h"
#import "ServersyncClass.h"

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
@interface Contacts ()

@end

@implementation Contacts
{
    ServersyncClass *serverSyncClassObject;
     general_data *generalDataObject;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    serverSyncClassObject=[ServersyncClass new];
    [NSThread detachNewThreadSelector:@selector(ServerSyncronizationMethod) toTarget:serverSyncClassObject withObject:nil];
   
   // appDelegate =  (AppDelegate *)[UIApplication sharedApplication].delegate;
 //   if(appDelegate.contactsObj)
  //      return;
    [super viewDidLoad];
    
    y=0;
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        y=y+20;
        
        
    }
    CGFloat width,height;
    width=self.view.frame.size.width;
    height=self.view.frame.size.height;
    // //NSLog(@" %f,%f",width,height);
    
    
    
    backgroundcolorview=[[UIView alloc]init];
    backgroundcolorview.frame=CGRectMake(0,y,width,height-100);
    [backgroundcolorview  setBackgroundColor:[UIColor colorWithPatternImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background" ofType:@"jpg"]]]];
    
    [self.view addSubview:backgroundcolorview];
    
    // [ContactsBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"contactsactive" ofType:@"png"]] forState:UIControlStateNormal];
    
    sidemenu=[[UIView alloc]init];
    sidemenu.frame=CGRectMake(0, 1,170 ,height-150);
    //sidemenu.hidden=YES;
    //[sidemenu  setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bluecolorstrip.jpg"]]];
    [sidemenu  setBackgroundColor:[UIColor colorWithPatternImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bluecolorstrip" ofType:@"jpg"]]]];
    
    
    NSString *pagetitle;
    pagetitle=@"Contacts";

    navBar = [[UINavigationBar alloc] init];
    navBar.frame=CGRectMake(0, 0,width , 50.0);
    
    [navBar setBackgroundImage:[UIImage imageNamed:@"logo-login-bg-short.png"] forBarMetrics:UIBarMetricsDefault];
    
    //[navBar setBackgroundColor:[UIColor blackColor]];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    titleLabel.font=[UIFont boldSystemFontOfSize:30.0];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text =pagetitle;
    [self.navigationItem setTitleView:titleLabel];
    
    
    //navItem.title = pagetitle;
    [navBar pushNavigationItem:self.navigationItem animated:false];
    
    [backgroundcolorview addSubview:navBar];
    
    @autoreleasepool {
        menubutton = [UIButton buttonWithType:UIButtonTypeCustom];
        [menubutton setFrame:CGRectMake(0,0,50,50)];
        [menubutton addTarget:self action:@selector(menuclicked) forControlEvents:UIControlEventTouchUpInside];
        
        [menubutton setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]] forState:UIControlStateNormal];
        [navBar addSubview:menubutton];
        
        
        logoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [logoBtn setFrame:CGRectMake(60,0,250,50)];
        // [logoBtn addTarget:self action:@selector(menuclicked) forControlEvents:UIControlEventTouchUpInside];
        
        //[logoBtn setImage:[UIImage imageNamed:@"headeraonelogo.png"] forState:UIControlStateNormal];
        [logoBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"headeraonelogo" ofType:@"png"]] forState:UIControlStateNormal];
        
        [navBar addSubview:logoBtn];
    }
   
    sationLbl=[[UILabel alloc] init];
    
    sationLbl.frame=CGRectMake(navBar.frame.size.width-280, 5,100, 40);
    
    [sationLbl setFont:[UIFont systemFontOfSize:13.0f]];
    [sationLbl setBackgroundColor:[UIColor clearColor]];
    [sationLbl setTextColor:[UIColor blackColor]];
    [sationLbl setText:@"Station No:1"];
    sationLbl.lineBreakMode = NSLineBreakByWordWrapping;
    sationLbl.numberOfLines = 2;
    [sationLbl setTextAlignment:NSTextAlignmentLeft];
    [navBar addSubview:sationLbl];
    
    
    EmpIdlbl=[[UILabel alloc] init];
    EmpIdlbl.frame=CGRectMake(navBar.frame.size.width-193, 5,150, 40);
    
    [EmpIdlbl setFont:[UIFont systemFontOfSize:13.0f]];
    [EmpIdlbl setBackgroundColor:[UIColor clearColor]];
    [EmpIdlbl setTextColor:[UIColor blackColor]];
    [EmpIdlbl setText:[NSString stringWithFormat:@"Employee Id: %@",[generalDataObject  userID]]];
    EmpIdlbl.lineBreakMode = NSLineBreakByWordWrapping;
    // EmpIdlbl.numberOfLines = 2;
    [EmpIdlbl setTextAlignment:NSTextAlignmentLeft];
    [navBar addSubview:EmpIdlbl];
    
    
    Timelbl=[[UILabel alloc] init];
    
    Timelbl.frame=CGRectMake(navBar.frame.size.width-62, 6,80, 40);
    
    [Timelbl setFont:[UIFont systemFontOfSize:13.0f]];
    [Timelbl setBackgroundColor:[UIColor clearColor]];
    [Timelbl setTextColor:[UIColor blackColor]];
    NSDate *aDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterShortStyle];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSString *datestr;
    datestr=[formatter stringFromDate:aDate];
    Timelbl.text=datestr;
    //Timelbl.text=[NSString stringWithFormat:@"%@:%@",@"Time",datestr];
    
    // timer is set & will be triggered each second
   // [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(showTime) userInfo:nil repeats:YES];
    Timelbl.lineBreakMode = NSLineBreakByWordWrapping;
    Timelbl.numberOfLines = 2;
    [Timelbl setTextAlignment:NSTextAlignmentLeft];
    [navBar addSubview:Timelbl];
    
    p=1;
    @autoreleasepool {
        
    for (int i=0; i< 10; i++)
    {
        
        categorybutton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [categorybutton setFrame:CGRectMake(0,p,170,40)];
        [categorybutton addTarget:self action:@selector(categotybuttonclicked:) forControlEvents:UIControlEventTouchUpInside];
        categorybutton.tag=i;
        if (categorybutton.tag == 0)
        {
            [categorybutton setImage:[UIImage imageNamed:@"posnormal2.png"] forState:UIControlStateNormal];
        }
        
        else if (categorybutton.tag == 1)
        {
            [categorybutton setImage:[UIImage imageNamed:@"profilenormal2.png"] forState:UIControlStateNormal];
        }
        else if (categorybutton.tag == 2)
        {
            [categorybutton setImage:[UIImage imageNamed:@"inventorynormal2.png"] forState:UIControlStateNormal];
        }
        else if(categorybutton.tag == 3)
        {
            [categorybutton setImage:[UIImage imageNamed:@"customernormal2.png"] forState:UIControlStateNormal];
        }
        else if (categorybutton.tag == 4)
        {
            [categorybutton setImage:[UIImage imageNamed:@"employeenormal2.png"] forState:UIControlStateNormal];
        }
        else if(categorybutton.tag == 5)
        {
            [categorybutton setImage:[UIImage imageNamed:@"settingsnormal2.png"] forState:UIControlStateNormal];
        }
        else if(categorybutton.tag == 6)
        {
            [categorybutton setImage:[UIImage imageNamed:@"reportsnormal2.png"] forState:UIControlStateNormal];
        }
        else if(categorybutton.tag == 7)
        {
            [categorybutton setImage:[UIImage imageNamed:@"contactsactive2.png"] forState:UIControlStateNormal];
        }
        else if (categorybutton.tag == 8)
        {
            [categorybutton setImage:[UIImage imageNamed:@"storesnormal.png"] forState:UIControlStateNormal];
        }
        
        else if (categorybutton.tag == 9)
        {
            //[categorybutton setImage:[UIImage imageNamed:@"bluecolorstrip.jpg"] forState:UIControlStateNormal];
        }
        [sidemenu addSubview:categorybutton];
        p=p+categorybutton.frame.size.height;
    }
    }
    mainview=[[UIView alloc]init];
    mainview.frame=CGRectMake(20,60,width-40,height-200);
    [mainview setBackgroundColor:[UIColor grayColor]];
    [mainview  setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"hqbackground69opacity.png"]]];
    [backgroundcolorview addSubview:mainview];

    logoimageview=[[UIImageView alloc]init];
    logoimageview.frame=CGRectMake(50, (backgroundcolorview.frame.size.height)+5+y,(height-(backgroundcolorview.frame.size.height)-30), (height-(backgroundcolorview.frame.size.height)-30));
    [logoimageview setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"aoneposlogo" ofType:@"png"]]];
    [self.view addSubview:logoimageview];
    
    helpiconButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [helpiconButton setFrame:CGRectMake(width-180,y+40,70,70)];
    [helpiconButton addTarget:self action:@selector(helpiconclicked) forControlEvents:UIControlEventTouchUpInside];
    
    //[helpiconButton setImage:[UIImage imageNamed:@"helpicon.png"] forState:UIControlStateNormal];
    [helpiconButton setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"helpicon" ofType:@"png"]] forState:UIControlStateNormal];
    
    [self.view addSubview:helpiconButton];
    logouticonButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [logouticonButton setFrame:CGRectMake(width-100,y+40,70,70)];
    [logouticonButton addTarget:self action:@selector(logoutButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    //[logouticonButton setImage:[UIImage imageNamed:@"logoutbutton.png"] forState:UIControlStateNormal];
    [logouticonButton setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"logoutbutton" ofType:@"png"]] forState:UIControlStateNormal];    [self.view addSubview:logouticonButton];

    ContanctsBackView=[[UIView alloc]init];
    ContanctsBackView.frame=CGRectMake(10, 0, mainview.frame.size.width-20, (mainview.frame.size.height));
    [ContanctsBackView setBackgroundColor:[UIColor whiteColor]];
    [ContanctsBackView.layer setBorderWidth: 1.0];
    [ContanctsBackView.layer setCornerRadius:8.0f];
    [ContanctsBackView.layer setMasksToBounds:YES];
    [ContanctsBackView.layer setBorderColor:[[UIColor blackColor] CGColor]];
    
    ContanctsBackView.layer.cornerRadius=8;
    [mainview addSubview:ContanctsBackView];
    
    
    ContactsView=[[UIView alloc]init];
    
    ContactsView.frame=CGRectMake(10, 10, ContanctsBackView.frame.size.width-20, ContanctsBackView.frame.size.height-40);
    [ContactsView setBackgroundColor:[UIColor colorWithRed:0.0f green:74.0f/255.0f blue:132.0f/255.0f alpha:1.0f]];
    
    ContactsView.layer.cornerRadius=8;
    [ContanctsBackView addSubview:ContactsView];
    
    
    //CGFloat contsctw=(ContactsView.frame.size.width)/2;
    
   if(!LicenceWebView)
    {
        @autoreleasepool {
            
   LicenceWebView=[[UIWebView alloc]initWithFrame:(CGRectMake(0, 0,ContactsView.frame.size.width, ContactsView.frame.size.height))];
    //[LicenceWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('info@aonepos.com').href"];
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"contactus" ofType:@"html"];
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    [LicenceWebView loadHTMLString:htmlString baseURL:nil];
    //NSURLRequest *urlRequest=[NSURLRequest requestWithURL:[generalDataObject  htmlurl]];
    
    //[LicenceWebView loadRequest:urlRequest];
   // LicenceWebView.delegate=self;
    //[LicenceWebView setBackgroundColor:[UIColor whiteColor]];
    [ContactsView addSubview:LicenceWebView];
        }
        }
    
    isLandScope = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
    [self updateUI];
    
     helpiconButton.hidden=YES;
    // Do any additional setup after loading the view from its nib.
}
-(void)viewWillAppear:(BOOL)animated
{
    isLandScope = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
    [self updateUI];
    
    [super viewWillAppear:YES];
}

-(void)showTime
{
    
    NSDate *aDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterShortStyle];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSString *datestr;
    datestr=[formatter stringFromDate:aDate];
    Timelbl.text=datestr;
    // Timelbl.text=[NSString stringWithFormat:@"%@:%@",@"Time",datestr];
    //Timelbl.text=[[NSDate date] description];
    
    
}


- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
        // //NSLog(@"Long Press");
    }
}
- (BOOL)shouldAutorotate
{
    // //NSLog(@"called");
    
    
    return YES;
    
    
}
-(void)updateUI
{
    
    if (isLandScope)
    {
        
        CGFloat width,height;
        
        width=1024;
        height=748;
        backgroundcolorview.frame=CGRectMake(0,y,width,height-100);
        
        
        //  sidemenu.frame=CGRectMake(0, 1,170 ,height-150);
        //sidemenu.hidden=YES;
        
        [categorybutton setFrame:CGRectMake(0.0,p,170,40)];
        navBar.frame=CGRectMake(0, 0,width , 50.0);
        
        [logoBtn setFrame:CGRectMake(60,0,250,50)];
        sationLbl.frame=CGRectMake(navBar.frame.size.width-280, 5,100, 40);
        EmpIdlbl.frame=CGRectMake(navBar.frame.size.width-193, 5,150, 40);
        Timelbl.frame=CGRectMake(navBar.frame.size.width-62, 6,80, 40);
        // mainview.frame=CGRectMake(sidemenu.frame.size.width+10,0,width-(sidemenu.frame.size.width+20),height-150);
        mainview.frame=CGRectMake(20,60,width-40,height-200);
        
        [backgroundcolorview addSubview:mainview];
        [menubutton setFrame:CGRectMake(0,0,50,50)];
        maintoolBar.frame = CGRectMake(0, 0, mainview.frame.size.width, 40);
        
        logoimageview.frame=CGRectMake(50, (backgroundcolorview.frame.size.height)+5+y,(height-(backgroundcolorview.frame.size.height)-30), (height-(backgroundcolorview.frame.size.height)-30));
        
        [helpiconButton setFrame:CGRectMake(width-180,(backgroundcolorview.frame.size.height)+10+y,70,70)];
        
        
        [logouticonButton setFrame:CGRectMake(width-100,(backgroundcolorview.frame.size.height)+10+y,70,70)];
    
        ContanctsBackView.frame=CGRectMake(10, 0, mainview.frame.size.width-20, (mainview.frame.size.height));
        ContactsView.frame=CGRectMake(10, 10, ContanctsBackView.frame.size.width-20, ContanctsBackView.frame.size.height-20);
        LicenceWebView.frame=(CGRectMake(0, 0,ContactsView.frame.size.width, ContactsView.frame.size.height));
    }
    else
    {
        CGFloat width,height;
        height=1004;
        width=768;
        // //NSLog(@" %f,%f",width,height);
        
        
        backgroundcolorview.frame=CGRectMake(0,y,width,height-100);
        navBar.frame=CGRectMake(0, 0,width , 50.0);
        [logoBtn setFrame:CGRectMake(60,0,250,50)];
        sationLbl.frame=CGRectMake(navBar.frame.size.width-280, 5,100, 40);
        EmpIdlbl.frame=CGRectMake(navBar.frame.size.width-193, 5,150, 40);
        Timelbl.frame=CGRectMake(navBar.frame.size.width-62, 6,80, 40);
        
        //sidemenu.frame=CGRectMake(0, 1,170 ,height-150);
        // sidemenu.hidden=YES;
        [categorybutton setFrame:CGRectMake(0.0,p,170,40)];
        mainview.frame=CGRectMake(20,60,width-40,height-200);
        [menubutton setFrame:CGRectMake(0,0,50,50)];
        
        maintoolBar.frame = CGRectMake(0, 0, mainview.frame.size.width, 40);
        //downtoolBar.frame = CGRectMake(0, 0, mainview.frame.size.width, 40);
        
        logoimageview.frame=CGRectMake(50, (backgroundcolorview.frame.size.height)+5+y,(height-(backgroundcolorview.frame.size.height)-30), (height-(backgroundcolorview.frame.size.height)-30));
        
        [helpiconButton setFrame:CGRectMake(width-180,(backgroundcolorview.frame.size.height)+10+y,70,70)];
        
        
        [logouticonButton setFrame:CGRectMake(width-100,(backgroundcolorview.frame.size.height)+10+y,70,70)];
        ContanctsBackView.frame=CGRectMake(10, 0, mainview.frame.size.width-20, (mainview.frame.size.height));
        
        
        ContactsView.frame=CGRectMake(10, 10, ContanctsBackView.frame.size.width-20, ContanctsBackView.frame.size.height-20);
        
       LicenceWebView.frame=(CGRectMake(0, 0,ContactsView.frame.size.width, ContactsView.frame.size.height));
        
    }
}

-(void)menuclicked
{
    if (menuslected == false)
    {
        
        // //NSLog(@"called");
        // //NSLog(@"%d",menuslected);
        menuslected=YES;
        logoBtn.hidden=YES;
        
        //[UIView beginAnimations:nil context:NULL];
        //[UIView setAnimationDuration:1.0f];
        sidemenu.transform = CGAffineTransformMakeTranslation(0,0);
        //[SideMenuView setBackgroundColor:[UIColor whiteColor]];
        //sidemenu.hidden=NO;
        //[UIView commitAnimations];
        [backgroundcolorview addSubview:sidemenu];
        
        //[UIView beginAnimations:nil context:NULL];
        //[UIView setAnimationDuration:0.9f];
        menubutton.transform = CGAffineTransformMakeTranslation(180,0);
        
        menubutton.transform = CGAffineTransformRotate( menubutton.transform, - M_PI);
        //[UIView commitAnimations];
      //  [UIView beginAnimations:nil context:NULL];
        //[UIView setAnimationDuration:1.0f];
        
        
    }else
    {
        
        menuslected=NO;
        logoBtn.hidden=NO;
       // [UIView beginAnimations:nil context:NULL];
        ///[UIView setAnimationDuration:1.0f];
        sidemenu.transform = CGAffineTransformMakeTranslation(-250,0);
        
       /// [UIView commitAnimations];
        
        
        
        
        ////[UIView beginAnimations:nil context:NULL];
        //[UIView setAnimationDuration:1.0f];
        menubutton.transform = CGAffineTransformMakeTranslation(0,0);
        
        
        //[UIView commitAnimations];
        //[UIView beginAnimations:nil context:NULL];
       /// [UIView setAnimationDuration:1.0f];
        
    }
    
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
    logoBtn.hidden=NO;
    //[UIView beginAnimations:nil context:NULL];
   // [UIView setAnimationDuration:0.8f];
    mainview.transform = CGAffineTransformMakeTranslation(0,0);
    
    
//[UIView commitAnimations];
    //[UIView beginAnimations:nil context:NULL];
    ////[UIView setAnimationDuration:0.8f];
    sidemenu.transform = CGAffineTransformMakeTranslation(-250,0);
    //[SideMenuView setBackgroundColor:[UIColor whiteColor]];
    
    ////[UIView commitAnimations];
    //[UIView commitAnimations];
    //[UIView beginAnimations:nil context:NULL];
//[UIView setAnimationDuration:0.0f];
    menubutton.transform = CGAffineTransformMakeTranslation(0,0);
    
    
    //[UIView commitAnimations];
//[UIView beginAnimations:nil context:NULL];
    ////[UIView setAnimationDuration:0.0f];
    menuslected=NO;
}

-(void)helpiconclicked
{
    
}
-(void)logoutButtonClicked
{
    [serverSyncClassObject LogoutMethod];
    NSString *updateSQL =@"UPDATE miscellaneous SET userType=?,sessionId=?,userID=?";
    NSMutableArray *misUpdatearray=[[NSMutableArray alloc]initWithObjects:@"",@"",@"", nil];
    [[DataBaseManager dataBaseManager] executeRecordsWithQuery:updateSQL withArguments:misUpdatearray];
    [self.navigationController popToRootViewControllerAnimated:YES];
    [generalDataObject  releasevariable];
    
    [self releaseViews];
}

-(void)categotybuttonclicked:(UIButton *)sender
{
    NSInteger selectedrow=sender.tag;
    NSArray *viewS = [self.navigationController viewControllers];
    //NSLog(@"nav views %@",viewS);
    
    if(selectedrow == 0){
        
        Home *homeview=[[Home alloc]initWithNibName:@"Home" bundle:Nil];
        NSString *returnvalue;
        NSInteger selectedview;
        for (int m=0;m<[self.navigationController.viewControllers count] ; m++)
        {
            if([[self.navigationController.viewControllers objectAtIndex:m] isKindOfClass:[Home class]])
            {
                returnvalue=@"yes";
                selectedview=m;
                break;
            }
            
        }
        ////NSLog(@"%d",selectedview);
        if([returnvalue isEqualToString:@"yes"])
        {
            [self.navigationController popToViewController:[viewS objectAtIndex:selectedview] animated:YES];
            [self releaseViews];
        }else{
            
            [self.navigationController pushViewController:homeview animated:NO];
            [self releaseViews];
        }
        
    }else if(selectedrow == 5){
        if ([[generalDataObject  userType] isEqualToString:@"employee"])
        {
            if ([[generalDataObject  settings_permission] isEqualToString:@"yes"])
            {
                [sender setImage:[UIImage imageNamed:@"settingsactive2.png"] forState:UIControlStateNormal];
                NSString *returnvalue;
                NSInteger selectedview;
                Settings  *settingsview=[[Settings alloc]initWithNibName:@"Settings" bundle:Nil];
                for (int m=0;m<[self.navigationController.viewControllers count] ; m++)
                {
                    if([[self.navigationController.viewControllers objectAtIndex:m] isKindOfClass:[Settings class]])
                    {
                        returnvalue=@"yes";
                        selectedview=m;
                        break;
                    }
                    
                }
                if([returnvalue isEqualToString:@"yes"])
                {
                    [self.navigationController popToViewController:[viewS objectAtIndex:selectedview] animated:YES];
                    [self releaseViews];
                }else
                {
                    
                    [self.navigationController pushViewController:settingsview animated:NO];
                    [self releaseViews];
                }
                
            }else{
                
                UIAlertView *authalert=[[UIAlertView alloc]initWithTitle:@"Authentication:" message:@"User not authenticated" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:Nil, nil];
                
                [authalert show];
            }
        }else{
            [sender setImage:[UIImage imageNamed:@"settingsactive2.png"] forState:UIControlStateNormal];
            NSString *returnvalue;
            NSInteger selectedview;
            Settings  *settingsview=[[Settings alloc]initWithNibName:@"Settings" bundle:Nil];
            for (int m=0;m<[self.navigationController.viewControllers count] ; m++)
            {
                if([[self.navigationController.viewControllers objectAtIndex:m] isKindOfClass:[Settings class]])
                {
                    returnvalue=@"yes";
                    selectedview=m;
                    break;
                }
                
            }
            if([returnvalue isEqualToString:@"yes"])
            {
                [self.navigationController popToViewController:[viewS objectAtIndex:selectedview] animated:YES];
                [self releaseViews];
            }else
            {
                [self.navigationController pushViewController:settingsview animated:NO];
                [self releaseViews];
            }
            
            
        }
        
        
    }else if(selectedrow == 8){
        NSString *returnvalue;
        NSInteger selectedview;
        Stores *storesview=[[Stores alloc]initWithNibName:@"Stores" bundle:Nil];
        for (int m=0;m<[self.navigationController.viewControllers count] ; m++)
        {
            if([[self.navigationController.viewControllers objectAtIndex:m] isKindOfClass:[Stores class]])
            {
                returnvalue=@"yes";
                selectedview=m;
                break;
            }
            
        }
        if([returnvalue isEqualToString:@"yes"])
        {
            [self.navigationController popToViewController:[viewS objectAtIndex:selectedview] animated:YES];
            [self releaseViews];
        }else
        {
            
            [self.navigationController pushViewController:storesview animated:NO];
            [self releaseViews];
        }
    }else if(selectedrow == 2){
        
        if ([[generalDataObject  userType] isEqualToString:@"employee"])
        {
            if ([[generalDataObject  inventory_permission] isEqualToString:@"yes"])
            {
                NSString *returnvalue;
                NSInteger selectedview;
                Inventory *inventoryview=[[Inventory alloc]initWithNibName:@"Inventory" bundle:Nil];
                for (int m=0;m<[self.navigationController.viewControllers count] ; m++)
                {
                    if([[self.navigationController.viewControllers objectAtIndex:m] isKindOfClass:[Inventory class]])
                    {
                        returnvalue=@"yes";
                        selectedview=m;
                        break;
                    }
                    
                }
                if([returnvalue isEqualToString:@"yes"])
                {
                    [self.navigationController popToViewController:[viewS objectAtIndex:selectedview] animated:YES];
                    [self releaseViews];
                }else
                {
                    
                    [self.navigationController pushViewController:inventoryview animated:NO];
                    [self releaseViews];
                }
                
            }else{
                
                UIAlertView *authalert=[[UIAlertView alloc]initWithTitle:@"Authentication:" message:@"User not authenticated" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:Nil, nil];
                
                [authalert show];
            }
        }
        else
        {
            
            NSString *returnvalue;
            NSInteger selectedview;
            Inventory *inventoryview=[[Inventory alloc]initWithNibName:@"Inventory" bundle:Nil];
            for (int m=0;m<[self.navigationController.viewControllers count] ; m++)
            {
                if([[self.navigationController.viewControllers objectAtIndex:m] isKindOfClass:[Inventory class]])
                {
                    returnvalue=@"yes";
                    selectedview=m;
                    break;
                }
                
            }
            if([returnvalue isEqualToString:@"yes"])
            {
                [self.navigationController popToViewController:[viewS objectAtIndex:selectedview] animated:YES];
                [self releaseViews];
            }else
            {
                
                [self.navigationController pushViewController:inventoryview animated:NO];
                [self releaseViews];
            }
        }
    }else if(selectedrow == 3){
        
        if ([[generalDataObject  userType] isEqualToString:@"employee"])
        {
            if ([[generalDataObject  customer_permission] isEqualToString:@"yes"])
            {
                NSString *returnvalue;
                NSInteger selectedview;
                Customers *customersview=[[Customers alloc]initWithNibName:@"Customers" bundle:Nil];
                for (int m=0;m<[self.navigationController.viewControllers count] ; m++)
                {
                    if([[self.navigationController.viewControllers objectAtIndex:m] isKindOfClass:[Customers class]])
                    {
                        returnvalue=@"yes";
                        selectedview=m;
                        break;
                    }
                    
                }
                if([returnvalue isEqualToString:@"yes"])
                {
                    [self.navigationController popToViewController:[viewS objectAtIndex:selectedview] animated:YES];
                    [self releaseViews];
                }else
                {
                    
                    [self.navigationController pushViewController:customersview animated:NO];
                    [self releaseViews];
                }
                
            }else{
                
                UIAlertView *authalert=[[UIAlertView alloc]initWithTitle:@"Authentication:" message:@"User not authenticated" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:Nil, nil];
                
                [authalert show];
            }
        }else{
            
            NSString *returnvalue;
            NSInteger selectedview;
            Customers *customersview=[[Customers alloc]initWithNibName:@"Customers" bundle:Nil];
            for (int m=0;m<[self.navigationController.viewControllers count] ; m++)
            {
                if([[self.navigationController.viewControllers objectAtIndex:m] isKindOfClass:[Customers class]])
                {
                    returnvalue=@"yes";
                    selectedview=m;
                    break;
                }
                
            }
            if([returnvalue isEqualToString:@"yes"])
            {
                [self.navigationController popToViewController:[viewS objectAtIndex:selectedview] animated:YES];
                [self releaseViews];
            }else
            {
                
                [self.navigationController pushViewController:customersview animated:NO];
                [self releaseViews];
            }
        }
        
    }else if(selectedrow == 4){
        if ([[generalDataObject  userType] isEqualToString:@"admin"])
        {
            NSString *returnvalue;
            NSInteger selectedview;
            Employees *employeesview=[[Employees alloc]initWithNibName:@"Employees" bundle:Nil];
            for (int m=0;m<[self.navigationController.viewControllers count] ; m++)
            {
                if([[self.navigationController.viewControllers objectAtIndex:m] isKindOfClass:[Employees class]])
                {
                    returnvalue=@"yes";
                    selectedview=m;
                    break;
                }
                
            }
            if([returnvalue isEqualToString:@"yes"])
            {
                [self.navigationController popToViewController:[viewS objectAtIndex:selectedview] animated:YES];
                [self releaseViews];
            }else
            {
                
                [self.navigationController pushViewController:employeesview animated:NO];
                [self releaseViews];
            }

            
        }
        else
        {
            UIAlertView *authalert=[[UIAlertView alloc]initWithTitle:@"Authentication:" message:@"User not authenticated" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:Nil, nil];
            
            [authalert show];
        }
        
    }else if(selectedrow == 6){
        
        if ([[generalDataObject  userType] isEqualToString:@"employee"])
        {
            if ([[generalDataObject  settings_permission] isEqualToString:@"yes"])
            {
                NSString *returnvalue;
                NSInteger selectedview;
                Reports *reportsview=[[Reports alloc]initWithNibName:@"Reports" bundle:Nil];
                for (int m=0;m<[self.navigationController.viewControllers count] ; m++)
                {
                    if([[self.navigationController.viewControllers objectAtIndex:m] isKindOfClass:[Reports class]])
                    {
                        returnvalue=@"yes";
                        selectedview=m;
                        break;
                    }
                    
                }
                if([returnvalue isEqualToString:@"yes"])
                {
                    [self.navigationController popToViewController:[viewS objectAtIndex:selectedview] animated:YES];
                    [self releaseViews];
                }else
                {
                    
                    [self.navigationController pushViewController:reportsview animated:NO];
                    [self releaseViews];
                }
                
            }else{
                
                UIAlertView *authalert=[[UIAlertView alloc]initWithTitle:@"Authentication:" message:@"User not authenticated" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:Nil, nil];
                
                [authalert show];
            }
        }else{
            
            NSString *returnvalue;
            NSInteger selectedview;
            Reports *reportsview=[[Reports alloc]initWithNibName:@"Reports" bundle:Nil];
            for (int m=0;m<[self.navigationController.viewControllers count] ; m++)
            {
                if([[self.navigationController.viewControllers objectAtIndex:m] isKindOfClass:[Reports class]])
                {
                    returnvalue=@"yes";
                    selectedview=m;
                    break;
                }
                
            }
            if([returnvalue isEqualToString:@"yes"])
            {
                [self.navigationController popToViewController:[viewS objectAtIndex:selectedview] animated:YES];
                [self releaseViews];
            }else
            {
                
                [self.navigationController pushViewController:reportsview animated:NO];
                [self releaseViews];
            }
            
        }
        
    }else if(selectedrow == 1){
        if ([[generalDataObject  userType] isEqualToString:@"admin"])
        {
            NSString *returnvalue;
            NSInteger selectedview;
            Profile *profileview=[[Profile alloc]initWithNibName:@"Profile" bundle:Nil];
            for (int m=0;m<[self.navigationController.viewControllers count] ; m++)
            {
                if([[self.navigationController.viewControllers objectAtIndex:m] isKindOfClass:[Profile class]])
                {
                    returnvalue=@"yes";
                    selectedview=m;
                    break;
                }
                
            }
            if([returnvalue isEqualToString:@"yes"])
            {
                [self.navigationController popToViewController:[viewS objectAtIndex:selectedview] animated:YES];
                [self releaseViews];
                
            }else
            {
                [self.navigationController pushViewController:profileview animated:NO];
                [self releaseViews];
            }
            
        }else{
            UIAlertView *authalert=[[UIAlertView alloc]initWithTitle:@"Authentication:" message:@"User not authenticated" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:Nil, nil];
            
            [authalert show];
            authalert=nil;
        }
    }
    
}

-(void)releaseViews
{
    NSArray *mainArray = self.view.subviews;
    for (int i = 0; i<[mainArray count]; i++) {
        UIView *vi =[mainArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    
    NSArray *sideArray = sidemenu.subviews;
    for (int i = 0; i<[sideArray count]; i++) {
        UIView *vi =[sideArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    
    NSArray *navBarArray = navBar.subviews;
    for (int i = 0; i<[navBarArray count]; i++) {
        UIView *vi =[navBarArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    NSArray *ContactsViewArray = ContactsView.subviews;
    for (int i = 0; i<[ContactsViewArray count]; i++) {
        UIView *vi =[ContactsViewArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    
    NSArray *ContanctsBackViewArray = ContanctsBackView.subviews;
    for (int i = 0; i<[ContanctsBackViewArray count]; i++) {
        UIView *vi =[ContanctsBackViewArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    [sidemenu removeFromSuperview],[mainview removeFromSuperview],[backgroundcolorview removeFromSuperview];
    [ContactsView removeFromSuperview],[ContanctsBackView removeFromSuperview];
    logoimageview.image=nil;
    sidemenu=nil,mainview=nil,backgroundcolorview=nil;
    categorybutton=nil;
    downtoolBar=nil,maintoolBar=nil;
    logobackview=nil,logoimageview=nil;
     helpiconButton=nil,logouticonButton=nil,menubutton=nil;
    backButton3=nil,backButton2=nil,backButton=nil;
    navBar=nil;
    ContanctsBackView=nil,ContactsView=nil;
    Titlelbl=nil,Textlbl=nil;
    
    [LicenceWebView loadHTMLString:Nil baseURL:nil];
    [LicenceWebView loadRequest:nil];
    [LicenceWebView removeFromSuperview];
    LicenceWebView=nil;
    
    
    
    sationLbl=nil,Timelbl=nil,EmpIdlbl=nil;
    logoBtn=nil;
    //profileview = nil;
    [self.view removeFromSuperview];
    self.view = nil;
    
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

{
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight||toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
        isLandScope=YES;
        
    }else
    {
        isLandScope=NO;
    }
    [self updateUI];
}



- (NSUInteger)supportedInterfaceOrientations // iOS 6 autorotation fix
{
    return UIInterfaceOrientationMaskAll;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    NSArray *viewS = [self.navigationController viewControllers];
    [self.navigationController popToViewController:[viewS objectAtIndex:1] animated:YES];
    [self releaseViews];
    // Dispose of any resources that can be recreated.
}

@end
